package com.sinodata.bsm.center.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.ResDimensionDao;
import com.sinodata.bsm.common.vo.QueryDimension;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-21 PM 4:37:33     dongyushi      1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class ResDimensionDaoImpl extends BaseDao implements ResDimensionDao {

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.center.dao.ResDimensionDao#findByPage(com.sinodata.bsm.center.bean.Page, com.sinodata.bsm.center.bean.Condition)
     */
    @Override
    public Page<QueryDimension> findByPage(Page<QueryDimension> page, Condition condition) {
        page.setTotal(getTotal());
        StringBuilder hql = new StringBuilder("from QueryDimension q where 1=1 ");
        hql.append(getConditonHql(condition));
        hql.append(getOrderSql(page));
        @SuppressWarnings("unchecked")
        List<QueryDimension> list = getSession().createQuery(hql.toString()).setFirstResult(page.getFirstResultNo()).setMaxResults(page.getPageSize()).list();
        page.setRows(list);
        return page;
    }

    /**
     * 获取排序sql
     * 
     * @param page
     * @return
     */
    private StringBuilder getOrderSql(Page<QueryDimension> page) {
        StringBuilder hql = new StringBuilder();
        Map<String, String> condMap = page.getOrder();
        if (condMap.size() != 0) {
            hql.append(" order by ");
            Iterator<String> keyItr = condMap.keySet().iterator();
            while (keyItr.hasNext()) {
                String key = keyItr.next();
                hql.append(key).append(" ").append(condMap.get(key));
                hql.append(" , ");
            }
            hql.delete(hql.lastIndexOf(" , "), hql.length());
        }
        return hql;
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.center.dao.ResDimensionDao#batchDelete(java.lang.Long[])
     */
    @Override
    public void batchDelete(Long[] ids) {
        this.getSession().createQuery("delete from QueryDimension where id in (:ids)").setParameterList("ids", ids).executeUpdate();
    }

    /**
     * 获取查询条件HQL
     * 
     * @param condition
     * @return
     */
    private StringBuilder getConditonHql(Condition condition) {
        StringBuilder hql = new StringBuilder();
        if (condition != null) {
            Map<String, String> condMap = condition.getWhere();
            if (condMap.size() != 0) {
                hql.append(" and ( ");
                Iterator<String> keyItr = condMap.keySet().iterator();
                while (keyItr.hasNext()) {
                    String key = keyItr.next();
                    hql.append(key).append(" like '%" + condMap.get(key).substring(1, condMap.get(key).lastIndexOf("'")) + "%'");
                    hql.append(" and ");
                }
                hql.delete(hql.lastIndexOf(" and "), hql.length()).append(" ) ");
            }
        } else {
            hql.append("");
        }
        return hql;
    }

    /**
     * 获取总数
     * 
     * @return
     */
    private int getTotal() {
        StringBuilder hql = new StringBuilder(" select count(*) from QueryDimension where 1=1 ");
        Query query = this.getSession().createQuery(hql.toString());
        return ((Long) query.list().iterator().next()).intValue();
    }

}